﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Public Class SumDataGridView
    Inherits UserControl
    ' Methods
    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Private Sub dgvDataList_ColumnWidthChanged(ByVal sender As Object, ByVal e As DataGridViewColumnEventArgs)
        Dim i As Integer
        For i = 0 To Me.DgvDataList.ColumnCount - 1
            Me.dgvTotalRow.Columns.Item(i).Width = Me.DgvDataList.Columns.Item(i).Width
        Next i
        Me.dgvTotalRow.HorizontalScrollingOffset = Me.DgvDataList.HorizontalScrollingOffset
    End Sub

    Private Sub dgvDataList_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs)
        Me.dgvTotalRow.ScrollBars = ScrollBars.None
        Me.dgvTotalRow.ColumnHeadersVisible = False
        Me.dgvTotalRow.AllowUserToAddRows = False
        Me.dgvTotalRow.AllowUserToDeleteRows = False
        Dim columnArray As DataGridViewColumn() = New DataGridViewColumn(Me.DgvDataList.ColumnCount - 1) {}
        Dim table As New DataTable
        Dim row As DataRow = table.NewRow
        Dim i As Integer
        For i = 0 To Me.DgvDataList.ColumnCount - 1
            columnArray(i) = New DataGridViewTextBoxColumn With { _
                .Name = Me.DgvDataList.Columns.Item(i).Name _
            }
            Dim column As New DataColumn(Me.DgvDataList.Columns.Item(i).Name)
            table.Columns.Add(column)
            row.Item(Me.DgvDataList.Columns.Item(i).Name) = ""
        Next i
        table.Rows.Add(row)
        Me.dgvTotalRow.DataSource = table
        If (Me.dgvTotalRow.RowCount > 0) Then
            Me.dgvTotalRow.Rows.Item(0).Selected = False
        End If
        Me.SetLastRowColor()
        Me.GetSumTotal()
        Me.SetScroll()
    End Sub

    Private Sub dgvDataList_Layout(ByVal sender As Object, ByVal e As LayoutEventArgs)
        Me.SetScroll()
    End Sub

    Private Sub dgvDataList_Resize(ByVal sender As Object, ByVal e As EventArgs)
        Me.SetScroll()
    End Sub

    Private Sub dgvDataList_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs)
        If (e.ScrollOrientation = ScrollOrientation.HorizontalScroll) Then
            Me.dgvTotalRow.HorizontalScrollingOffset = e.NewValue
        End If
    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If (disposing AndAlso (Not Me.components Is Nothing)) Then
            Me.components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    Public Sub GetSumTotal()
        Dim num As Decimal = 0
        If (Not Me.SumColumnList Is Nothing) Then
            Dim str As String
            For Each str In Me.SumColumnList
                num = 0
                Dim row As DataGridViewRow
                For Each row In DirectCast(Me.DgvDataList.Rows, IEnumerable)
                    If row.Visible Then
                        num = (num + If(((Not row.Cells.Item(str).Value Is Nothing) AndAlso (Not row.Cells.Item(str).Value Is DBNull.Value)), Convert.ToDecimal(row.Cells.Item(str).Value), 0))
                    End If
                Next
                Me.dgvTotalRow.Rows.Item(0).Cells.Item(str).Value = num
            Next
            If ((Me.ShowTotal >= 0) AndAlso (Me.ShowTotal < Me.dgvTotalRow.ColumnCount)) Then
                Me.dgvTotalRow.Rows.Item(0).Cells.Item(Me.ShowTotal).Value = ChrW(21512) & ChrW(35745)
            End If
        End If
    End Sub

    Private Sub InitializeComponent()
        Dim style As New DataGridViewCellStyle
        Dim style2 As New DataGridViewCellStyle
        Me.dgvTotalRow = New DataGridView
        DirectCast(Me.dgvTotalRow, ISupportInitialize).BeginInit()
        MyBase.SuspendLayout()
        Me.dgvTotalRow.AllowUserToAddRows = False
        Me.dgvTotalRow.AllowUserToDeleteRows = False
        Me.dgvTotalRow.BorderStyle = BorderStyle.None
        Me.dgvTotalRow.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
        style.Alignment = DataGridViewContentAlignment.MiddleLeft
        style.BackColor = SystemColors.Window
        style.Font = New Font(ChrW(23435) & ChrW(20307), 9.0!, FontStyle.Regular, GraphicsUnit.Point, &H86)
        style.ForeColor = SystemColors.ControlText
        style.SelectionBackColor = SystemColors.HighlightText
        style.SelectionForeColor = SystemColors.ControlText
        style.WrapMode = DataGridViewTriState.False
        Me.dgvTotalRow.DefaultCellStyle = style
        Me.dgvTotalRow.Dock = DockStyle.Fill
        Me.dgvTotalRow.Location = New Point(0, 0)
        Me.dgvTotalRow.Name = "dgvTotalRow"
        Me.dgvTotalRow.ReadOnly = True
        style2.Alignment = DataGridViewContentAlignment.MiddleLeft
        style2.BackColor = SystemColors.Control
        style2.Font = New Font(ChrW(23435) & ChrW(20307), 9.0!, FontStyle.Regular, GraphicsUnit.Point, &H86)
        style2.ForeColor = SystemColors.WindowText
        style2.SelectionBackColor = SystemColors.Control
        style2.SelectionForeColor = SystemColors.WindowText
        style2.WrapMode = DataGridViewTriState.True
        Me.dgvTotalRow.RowHeadersDefaultCellStyle = style2
        Me.dgvTotalRow.RowTemplate.Height = &H17
        Me.dgvTotalRow.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        Me.dgvTotalRow.Size = New Size(&H11B, &H19)
        Me.dgvTotalRow.TabIndex = 4
        MyBase.AutoScaleDimensions = New SizeF(6.0!, 12.0!)
        MyBase.AutoScaleMode = AutoScaleMode.Font
        MyBase.Controls.Add(Me.dgvTotalRow)
        MyBase.Name = "SumDataGridView"
        MyBase.Size = New Size(&H11B, &H19)
        AddHandler MyBase.Load, New EventHandler(AddressOf Me.UserSumDataGridView_Load)
        DirectCast(Me.dgvTotalRow, ISupportInitialize).EndInit()
        MyBase.ResumeLayout(False)
    End Sub

    Public Sub SetLastRowColor()
        If (Me.DgvDataList.RowCount > 0) Then
            Me.DgvDataList.Rows.Item((Me.DgvDataList.RowCount - 1)).DefaultCellStyle.SelectionBackColor = Me.DgvDataList.BackgroundColor
            Me.DgvDataList.Rows.Item((Me.DgvDataList.RowCount - 1)).DefaultCellStyle.SelectionForeColor = Me.DgvDataList.BackgroundColor
            Me.DgvDataList.Rows.Item((Me.DgvDataList.RowCount - 1)).DefaultCellStyle.BackColor = Me.DgvDataList.BackgroundColor
            Me.DgvDataList.Rows.Item((Me.DgvDataList.RowCount - 1)).DefaultCellStyle.ForeColor = Me.DgvDataList.BackgroundColor
        End If
    End Sub

    Public Sub SetScroll()
        MyBase.Height = &H15
        Dim x As Integer = (Me.DgvDataList.Location.X + 1)
        Dim num2 As Integer = (Me.DgvDataList.Height + Me.DgvDataList.Location.Y)
        If (Me.DgvDataList.DisplayedRowCount(True) <> Me.DgvDataList.RowCount) Then
            MyBase.Width = (Me.DgvDataList.Width - &H13)
        Else
            MyBase.Width = Me.DgvDataList.Width
        End If
        If (Me.DgvDataList.DisplayedColumnCount(True) <> Me.DgvDataList.ColumnCount) Then
            MyBase.Location = New Point(x, ((num2 - Me.dgvTotalRow.Height) - &H13))
        Else
            MyBase.Location = New Point(x, (num2 - Me.dgvTotalRow.Height))
        End If
    End Sub

    Private Sub UserSumDataGridView_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.DgvDataList.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
        Me.dgvTotalRow.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
        AddHandler Me.DgvDataList.Scroll, New ScrollEventHandler(AddressOf Me.dgvDataList_Scroll)
        AddHandler Me.DgvDataList.ColumnWidthChanged, New DataGridViewColumnEventHandler(AddressOf Me.dgvDataList_ColumnWidthChanged)
        AddHandler Me.DgvDataList.Layout, New LayoutEventHandler(AddressOf Me.dgvDataList_Layout)
        AddHandler Me.DgvDataList.Resize, New EventHandler(AddressOf Me.dgvDataList_Resize)
        AddHandler Me.DgvDataList.DataBindingComplete, New DataGridViewBindingCompleteEventHandler(AddressOf Me.dgvDataList_DataBindingComplete)
    End Sub


    ' Properties
    Public Property DgvDataList As DataGridView
    Public Property ShowTotal As Integer
    Public Property SumColumnList As IList(Of String)

    ' Fields
    Private components As IContainer = Nothing
    Public dgvTotalRow As DataGridView
End Class